% by YLX 6/11
% update to 4.0
function Q = findQ(S)
    %st = ftimer();
    global I CURRENT
    m_max = CURRENT; % total number of patients by now
    Q = zeros(I, m_max+1);
    for m = 0:m_max
        PIE = findPIE(m); % global PIE filled Here
        % display(PIE)
        for i = 1:I
            prunedPIE = prunePIE(PIE, S(i, :)); 
            if size(prunedPIE, 1) == 0
                continue
            end
            Q(i, m+1) = elementQ(S, i, prunedPIE);
        end
    end

    %global T_findQ N_findQ;
    %T_findQ = T_findQ + ftimer()-st;
    %N_findQ = N_findQ + 1;
    return;